<?php

/**
*
*/

function encode($value){
	$encoder = new Encoder();
	$encoder->setEncoders(array('md5', 'sha1'));
	$encoder->setSalt("hjkshd4454%*fd56");
	return $encoder->crypt($value);
}
function encrypt($data) {
	$key = "kio68Ki%";
	$data = serialize($data);
	$td = mcrypt_module_open(MCRYPT_DES,"",MCRYPT_MODE_ECB,"");
	$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
	mcrypt_generic_init($td,$key,$iv);
	$data = base64_encode(mcrypt_generic($td, '!'.$data));
	mcrypt_generic_deinit($td);
	return $data;
}

function decrypt($data) {
	$key = "kio68Ki%";
	$td = mcrypt_module_open(MCRYPT_DES,"",MCRYPT_MODE_ECB,"");
	$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
	mcrypt_generic_init($td,$key,$iv);
	$data = mdecrypt_generic($td, base64_decode($data));
	mcrypt_generic_deinit($td);

	if (substr($data,0,1) != '!')
		return false;

	$data = substr($data,1,strlen($data)-1);
	return unserialize($data);
}
class Encoder
{
    private $encoders;
    private $salt ="";
    public function __construct()
    {
    }
    public function setEncoders($encoders)
    {
        if (!is_array($encoders)) {
            $this->encoders = array($encoders);
        } else {
            $this->encoders = $encoders;
        }
    }

    public function crypt($text)
    {
        if (empty($this->encoders) || in_array('plaintext', $this->encoders)) {
            return $text;
        }
        $encoderAvailable = hash_algos();
        foreach ($this->encoders as $encoder) {
            if (!in_array($encoder, $encoderAvailable)) {
                continue;
            }
            $text = hash($encoder, $text.$this->salt);
        }

        return $text;
    }
	public function getSalt() {
		return $this->salt;
	}
	public function setSalt($salt) {
		$this->salt = $salt;
		return $this;
	}


}
